import type { ISupplyDemandDetailsResult } from "@/api/businessOpportunities/type";
import type { WxShareOptions } from "@/components/CustomShare/types";
import { CooperationDurationTypeEnum, IncentiveTypeEnum, type IncentiveOpenEnum } from "@/enums/businessOpportunitiesEnum";
import { useUserStoreWithOut } from "@/store/modules/user";
import { formatToDate } from "@/utils/dateUtil";

export const SupplyDemandFormAnswerCacheKey = 'SupplyDemandFormAnswerCache'
const emptyAvatar = 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/icon/emptyIcon/empty-avatar.png'

export interface IncentiveDetailsOptions {
  /** 预估规模 */
  estimatedScale: string;
  /** 撮合开关；0:否;1是 */
  incentiveOpen: IncentiveOpenEnum;
  /** 撮合激励类型: 1佣金激励；2:其他激励 */
  incentiveType: IncentiveTypeEnum;
  /** 撮合说明 */
  showIncentiveDetails: string;
  /** 撮合佣金比例 */
  showCommissionRate: string;
}
/** 激励说明 */
export function getIncentiveDetails(data: IncentiveDetailsOptions) {
  if (!data?.incentiveOpen) return '';
  if (data.incentiveType === IncentiveTypeEnum.Other) return data.showIncentiveDetails || '无';
  let str = data.estimatedScale ? `预估规模${data.estimatedScale}` : ''
  if (data.estimatedScale && data.showCommissionRate) str += ','
  return str + `成交佣金${data.showCommissionRate || 0}%`;
}

type ShareDataType = Pick<ISupplyDemandDetailsResult,
  'title' | 'publishTime' | 'cooperationTypeName' | 'cooperationDatetime' | 'cooperationDurationType' |
  'industryCategoryNames' | 'provinceName' | 'cityName' | 'areaName'
>

export const getAddressText = (data: ShareDataType) => {
  if (!data?.provinceName && !data?.cityName) return '不限地区';
  return `${data?.provinceName || ''} ${data?.cityName || ''} ${data?.areaName || ''}`
}
const getCooperationDurationType = (data: ShareDataType) => {
  if (data?.cooperationDurationType === CooperationDurationTypeEnum.LimitedDate) {
    return formatToDate(data?.cooperationDatetime)
  }
  else return '长期合作'
}

/**
 * @description 供需分享图数据
 * @date 09/12/2024
 * @export
 * @param {ShareDataType} data
 * @returns {*}  {PainterBoard}
 */
export function getSupplyDemandWxShareData(data: ShareDataType, path: string): WxShareOptions {
  // const addressText = getAddressText(data);
  // const cooperationDurationText = getCooperationDurationType(data);
  const [w, h] = [480, 384]
  const pd = 48

  return {
    path,
    title: '我发现一条优质资源信息,快来看看吧~',
    board: {
      css: {
        width: w + 'rpx',
        height: h + 'rpx',
        position: 'relative'
      },
      views: [
        {
          type: 'image',
          src: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/sj_bg.png',
          css: {
            width: '100%',
            height: '100%'
          }
        },
        {
          type: 'view',
          css: {
            position: 'absolute',
            left: pd + 'rpx',
            top: pd + 'rpx',
            width: (w - pd * 2) + 'rpx'
          },
          views: [
            {
              type: 'text',
              text: data.cooperationTypeName,
              css: {
                display: 'inline-block',
                padding: '0 12rpx',
                height: '36rpx',
                lineHeight: '36rpx',
                fontSize: '20rpx',
                background: '#EA5E0E',
                color: '#fff',
                borderRadius: '4rpx',
                marginBottom: '16rpx'
              }
            },
            {
              type: 'text',
              text: data.title,
              css: {
                display: 'block',
                color: '#333',
                fontSize: '28rpx',
                lineClamp: 3,
                fontWeight: 'bold'
              }
            }
          ]
        }
      ]
    }
    // board: {
    //   css: {
    //     width: '480rpx',
    //     height: '384rpx',
    //     padding: '20rpx',
    //     background: '#EBEDF0'
    //   },
    //   views: [
    //     {
    //       text: data.title,
    //       type: 'text',
    //       css: {
    //         lineClamp: 2,
    //         fontSize: '24rpx',
    //         fontWeight: 'bold',
    //         color: '#333',
    //         marginBottom: '16rpx',
    //         display: 'block'
    //       }
    //     },
    //     {
    //       text: formatToDate(data?.publishTime) + '发布',
    //       type: 'text',
    //       css: {
    //         fontSize: '20rpx',
    //         color: '#969799',
    //         display: 'block',
    //         marginBottom: '32rpx'
    //       }
    //     },
    //     {
    //       type: 'view',
    //       css: {
    //         marginBottom: '16rpx'
    //       },
    //       views: [
    //         {
    //           text: '合作类型：',
    //           type: 'text',
    //           css: {
    //             width: '110rpx',
    //             fontSize: '20rpx',
    //             color: '#333'
    //           }
    //         },
    //         {
    //           text: data.cooperationTypeName,
    //           type: 'text',
    //           css: {
    //             fontSize: '20rpx',
    //             color: '#333'
    //           }
    //         }
    //       ]
    //     },
    //     {
    //       type: 'view',
    //       css: {
    //         marginBottom: '16rpx'
    //       },
    //       views: [
    //         {
    //           text: '行业要求：',
    //           type: 'text',
    //           css: {
    //             width: '110rpx',
    //             fontSize: '20rpx',
    //             color: '#333'
    //           }
    //         },
    //         {
    //           text: data.industryCategoryNames,
    //           type: 'text',
    //           css: {
    //             width: '320rpx',
    //             fontSize: '20rpx',
    //             color: '#333',
    //             lineClamp: 2
    //           }
    //         }
    //       ]
    //     },
    //     {
    //       type: 'view',
    //       css: {
    //         marginBottom: '16rpx'
    //       },
    //       views: [
    //         {
    //           text: '合作期限：',
    //           type: 'text',
    //           css: {
    //             width: '110rpx',
    //             fontSize: '20rpx',
    //             color: '#333'
    //           }
    //         },
    //         {
    //           text: cooperationDurationText,
    //           type: 'text',
    //           css: {
    //             fontSize: '20rpx',
    //             color: '#333'
    //           }
    //         }
    //       ]
    //     },
    //     {
    //       type: 'view',
    //       css: {
    //         marginBottom: '16rpx'
    //       },
    //       views: [
    //         {
    //           text: '合作地区：',
    //           type: 'text',
    //           css: {
    //             width: '110rpx',
    //             fontSize: '20rpx',
    //             color: '#333'
    //           }
    //         },
    //         {
    //           text: addressText,
    //           type: 'text',
    //           css: {
    //             fontSize: '20rpx',
    //             color: '#333'
    //           }
    //         }
    //       ]
    //     },
    //   ]
    // }
  }
}

/**
 * @description 供需海报数据
 * @date 09/12/2024
 * @export
 * @param {ShareDataType} data
 * @param {string} codeImg
 * @returns {*}  {PainterBoard}
 */
export function getSupplyDemandPosterData(data: ShareDataType, codeImg: string): PainterBoard {
  const addressText = getAddressText(data);
  const cooperationDurationText = getCooperationDurationType(data);
  const { userInfo } = useUserStoreWithOut()

  return {
    css: {
      width: '800rpx',
      height: '1024rpx',
      background: '#F2F3F5',
    },
    views: [
      {
        type: 'view',
        css: {
          padding: '40rpx',
        },
        views: [
          {
            type: 'view',
            css: { marginBottom: '32rpx' },
            views: [
              {
                type: 'image',
                src: userInfo.avatar || emptyAvatar,
                css: {
                  width: '96rpx',
                  height: '96rpx',
                  borderRadius: '50%',
                  objectFit: 'cover'
                }
              },
              {
                type: 'text',
                text: `Hi 我是${userInfo.userName} \n我发现一条优质资源信息，快来看看吧`,
                css: {
                  width: '608rpx',
                  color: '#333',
                  fontSize: '28rpx',
                  lineHeight: '44rpx',
                  marginLeft: '16rpx'
                }
              },
            ]
          },
          {
            type: 'view',
            css: {
              background: '#F7F8FA',
              padding: '28rpx',
              border: '1px solid #EBEDF0',
              boxShadow: '0px 96rpx 48rpx rgba(185, 209, 245, 0.40)',
              marginBottom: '40rpx'
            },
            views: [
              {
                text: data.title,
                type: 'text',
                css: {
                  lineClamp: 2,
                  fontSize: '36rpx',
                  fontWeight: 'bold',
                  color: '#333',
                  marginBottom: '24rpx',
                  display: 'block'
                }
              },
              {
                text: formatToDate(data?.publishTime) + '发布',
                type: 'text',
                css: {
                  fontSize: '28rpx',
                  color: '#969799',
                  display: 'block',
                  marginBottom: '48rpx'
                }
              },
              {
                type: 'view',
                css: {
                  marginBottom: '24rpx',
                },
                views: [
                  {
                    text: '合作类型：',
                    type: 'text',
                    css: {
                      width: '150rpx',
                      fontSize: '28rpx',
                      color: '#333'
                    }
                  },
                  {
                    text: data.cooperationTypeName,
                    type: 'text',
                    css: {
                      fontSize: '28rpx',
                      color: '#333'
                    }
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  marginBottom: '24rpx',
                },
                views: [
                  {
                    text: '行业要求：',
                    type: 'text',
                    css: {
                      width: '150rpx',
                      fontSize: '28rpx',
                      color: '#333'
                    }
                  },
                  {
                    text: data.industryCategoryNames,
                    type: 'text',
                    css: {
                      width: '506rpx',
                      fontSize: '28rpx',
                      color: '#333',
                      lineClamp: 2
                    }
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  marginBottom: '24rpx',
                },
                views: [
                  {
                    text: '合作期限：',
                    type: 'text',
                    css: {
                      width: '150rpx',
                      fontSize: '28rpx',
                      color: '#333'
                    }
                  },
                  {
                    text: cooperationDurationText,
                    type: 'text',
                    css: {
                      fontSize: '28rpx',
                      color: '#333'
                    }
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  marginBottom: '24rpx',
                },
                views: [
                  {
                    text: '合作地区：',
                    type: 'text',
                    css: {
                      width: '150rpx',
                      fontSize: '28rpx',
                      color: '#333'
                    }
                  },
                  {
                    text: addressText,
                    type: 'text',
                    css: {
                      fontSize: '28rpx',
                      color: '#333'
                    }
                  }
                ]
              },
            ]
          }
        ]
      },
      {
        type: 'view',
        css: {
          width: '100%',
          position: 'absolute',
          right: '0',
          bottom: '0',
          textAlign: 'right',
          background: '#fff',
          padding: '40rpx 0'
        },
        views: [
          {
            type: 'text',
            text: '微信扫一扫查看详情',
            css: {
              fontSize: '28rpx',
              color: '#999996',
              lineHeight: '160rpx',
              marginRight: '16rpx'
            }
          },
          {
            type: 'image',
            src: codeImg,
            css: {
              width: '160rpx',
              height: '160rpx',
              marginRight: '40rpx'
            }
          }
        ]
      }
    ]
  }
}